package com.yonyou.pmclouds.signin.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.signin.entity.SignInVO;
import com.yonyou.pmclouds.signin.mapper.SignInMapper;
import com.yonyou.pmclouds.signin.processor.SignInProjectLogPostProcess;
import com.yonyou.pmclouds.signin.processor.SignInInsertCheckProcessor;
import com.yonyou.pmclouds.signin.rmiitf.SignInMaintain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= SignInMaintain.class)
public class SignInMaintainImpl implements SignInMaintain{

    @Autowired
    private SignInMapper mapper;
    @Autowired
    private SignInProjectLogPostProcess signInProjectLogPostProcess;


    @Override
    public SignInVO insert(SignInVO signInVO) throws BusinessException {
        if(signInVO == null){
            return null;
        }
        initInsertVO(signInVO);//补全字段信息
        SingleInsertBpTemplate<SignInVO> bp = new SingleInsertBpTemplate<>(mapper);
        //更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<SignInVO>());
        bp.addBeforeProcessor(new SignInInsertCheckProcessor());
        //智能日志
        bp.addAfterProcessor(signInProjectLogPostProcess);
        bp.insert(signInVO);
        return signInVO;
    }


    private void initInsertVO(SignInVO vo){

        if(vo == null) return;
        //单据类型
        vo.setBill_type(BillTypeConst.SIGN_IN_BILLTYPE);
        //租户
        if(StringUtils.isEmpty(vo.getPkTenant())){
            vo.setPkTenant(RuntimeEnvironment.getTenantId());
        }
        /**
         * 在下一次迭代需要对是否迟到(inTime)字段进行赋值
         */
    }
}
